package io.shdxhl.simplerpc.code.netty.handler;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.shdxhl.simplerpc.code.netty.RpcRequestPromise;
import io.shdxhl.simplerpc.code.netty.response.RpcResponse;
import io.shdxhl.simplerpc.code.utils.RequestUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

@Slf4j
@ChannelHandler.Sharable
public class RpcResponseHandler extends SimpleChannelInboundHandler<RpcResponse> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, RpcResponse response) throws Exception {
        log.info("RpcResponseHandler 请求返回:{}", response);
        RpcRequestPromise request = RequestUtil.getRequest(response.getRequestId());
        if(Objects.nonNull(request)){
            request.setSuccess(response);
        }
    }
}
